package com.ybu.practice;

import java.util.ArrayList;
import java.util.Scanner;

public class ManagementSystem {
    //创建一个集合存放学生信息
    static  ArrayList<Student> students = new ArrayList<>();
    //创建一个Scanner对象
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        students.add(new Student("张三", 20, 1001, "北京"));
        students.add(new Student("李四", 21, 1002, "上海"));
        students.add(new Student("王五", 22, 1003, "广州"));
        while (true) {
            System.out.println("-----欢迎使用学生管理系统------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    queryStudent();
                    break;
                case 5:
                    System.out.println("退出系统！");
                    return;
                default:
                    System.out.println("输入信息有误，请重新输入！");
            }
        }
    }
    //获取集合中的学生id
    public static int getId(){
        for (Student student : students) {
            return student.getId();
        }
        return 0;
    }
    //添加学生方法
    public static void addStudent(){
        System.out.println("请输入学号：");
        //检测是否学号重复

        int id = scanner.nextInt();
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入地址：");
        String address = scanner.next();
        //将添加的学生存储到students集合中
        students.add(new Student(name,age,id,address));
    }
    //删除学生方法
    public static void deleteStudent(){
        System.out.println("请输入要删除学生的学号：");
        int id = scanner.nextInt();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() == id) {
                students.remove(i);
                System.out.println("删除成功！");
            }
        }
    }
    //修改学生方法
    public static void updateStudent() {
        System.out.println("请输入要修改学生的学号：");
        int targetId = scanner.nextInt();

        // 查找匹配ID的学生
        Student foundStudent = null;

        for (Student student : students) {
            if (student.getId() == targetId) {
                foundStudent = student;
                break;
            }
        }

        // 检查是否找到学生
        if (foundStudent == null) {
            System.out.println("没有找到学号为 " + targetId + " 的学生！");
            return;
        }

        // 更新学生信息
        System.out.println("请输入姓名：");
        foundStudent.setName(scanner.next());

        System.out.println("请输入年龄：");
        foundStudent.setAge(scanner.nextInt());

        System.out.println("请输入地址：");
        foundStudent.setAddress(scanner.next());

        System.out.println("修改成功！");
    }
    //查询学生方法
    public static void queryStudent(){
        if (students.size()==0){
            System.out.println("没有学生！");
        }
        for (Student student : students) {
            System.out.println(student.getId() + "   " + student.getName() + "   " + student.getAge() + "岁   " + student.getAddress());
        }
    }
}
